// let p1 = new Promise((resolve, reject) => {
//   resolve('ok')
//   throw('报错')
// })
// console.log(p1);


// resolve()  Promise的状态会变成fulfilled
// reject()  Promise的状态会变成rejected
// Promise的状态只能变更一次
// throw() 相当于 reject()

class MyPromise {
	constructor(executor) {
		this.status = 'pending'
		this.value = null
		let _this = this

		function resolve(value) {
			if (_this.status !== 'pending') return
			_this.status = 'fulfilled'
			_this.value = value
		}

		function reject(reason) {
			if (_this.status !== 'pending') return
			_this.status = 'rejected'
			_this.value = reason
		}

		try {
			executor(resolve, reject)
		} catch (error) {
			reject(error)
		}
	}
}

// let p1 = new MyPromise((resolve, reject) => {
// 	// resolve('ok')
// 	reject('err')
// 	// throw ('err')
// })
// let p1 = new MyPromise()
// p1.resolve('ok')
// console.log(p1);

let p = MyPromise.resolve(123)
console.log(p);
